<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>도서 검색</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript">

$(document).ready(function(){
	$("#select").live("click", function(){
		var txt="";
		if($("#select").val()=="sn"||$("#select").val()=="name"||$("#select").val()=="writer"){
			$("#first_layer").empty().append("<input type='text' name='text' id='text'/>");
		}else if($("#select").val()=="all"){
			$("#first_layer").empty();
		//상태
		}else if($("#select").val()=="state"){
			txt="<select name='state' id='state'><option value='default'>상태 선택</option><option value='0'>보관중</option>"+
					"<option value='1'>대여중</option><option value='2'>예약중</option>";
			$("#first_layer").html(txt);
		}//대분류
		else if($("#select").val()=="category"){
			$.ajax({
				url:"searchBookByCategory2.do",
				type:"GET",
				dataType:"JSON",
				success:function(jobj, msg, xhr){
					var list = jobj.list;
					txt="<select name='high' id='high'><option value='default'>대분류 선택</option>";
					$.each(list, function(idx, obj){ 
						txt = txt+"<option value="+obj.category_high_id+">"+obj.category_high_name+"</option>";
					})
					$("#first_layer").html(txt);
				},
				error:function(xhr){
					alert("오류가 발생 :"+xhr.status);
				}
			});
		}
	});
	//중분류
	$("#high").live("click", function(){
		var txt="";
		if($("#high").val()!="default"){
			$("#low").empty().append("<option value='default'>소분류 선택</option>");
			$.ajax({
				url:"searchBookByMiddleCategory.do?categoryHighId="+$("#high").val(),
				type:"GET",
				dataType:"JSON",
				success:function(jobj, msg, xhr){
					var list = jobj.list; 
					txt="<select name='middle' id='middle'><option value='default'>중분류 선택</option>";
					$.each(list, function(idx, obj){
						txt = txt+"<option value="+obj.category_Middle_Id+">"+obj.category_Middle_Name+"</option>";
					})
					$("#first_layer").append(txt);
				},
				error:function(xhr){
					alert("오류가 발생 :"+xhr.status);
				}
			})
		}
	});
	//소분류
	$("#middle").live("click", function(){
		var txt="";
		if($("#middle").val()!="default"){
			$.ajax({
				url:"searchBookByLowCategory.do?categoryLowId="+$("#middle").val(),
				type:"GET",
				dataType:"JSON",
				success:function(jobj, msg, xhr){
					var list = jobj.list;
					txt="<select name='low' id='low'><option value='default'>소분류 선택</option>";
					$.each(list, function(idx, obj){
						txt = txt+"<option value="+obj.category_Low_Id+">"+obj.category_Low_Name+"</option>";
					});
					$("#first_layer").append(txt);
				},
				error:function(xhr){
					alert("오류가 발생 :"+xhr.status);
				}
			});
		}
	});
	$("#btn").live("click", function(){
		var txt="";
		if($("#select").val()=="sn"){
			txt="searchBookBySN.do";
		}else if($("#select").val()=="name"){
			txt="searchBookByName2.do";
		}else if($("#select").val()=="writer"){
			txt="searchBookByWriter.do";
		}else if($("#select").val()=="all"){
			txt="searchBookAll.do";
		}
		if($("#select").val()=="sn"||$("#select").val()=="name"||$("#select").val()=="writer"||$("#select").val()=="all"){
			$.ajax({
				url:txt,
				type:"POST",
				dataType:"JSON",
				data:$("#select").val()+"="+$("#text").val(),
				success:function(jobj, msg, xhr){
					var list = jobj.list;
					var txt="";
					$.each(list, function(idx, obj){
						txt = txt+"sn : "+obj.bookSN+", 제목 : "+obj.name+", 저자 : "+obj.writer+", 출판사 : "+obj.publisher+
						"<a href='bookModify.do??sn="+obj.bookSN+"'>수정</a>&nbsp;&nbsp;"+
						"<a href='aaaaaaa'>삭제</a>"+"<br>";
					});
					$("#result_layer").html(txt);
				},
				error:function(xhr){
					alert("오류가 발생 :"+xhr.status);
				}
			});
		}
		if($("#select").val()=="category"&&$("#high").val()!="default"&&$("#middle").val()!="default"&&$("#low").val()!="default"){
			$.ajax({
				url:"searchBookListByCategory.do",
				type:"POST",
				dataType:"JSON",
				data:"categoryLowId="+$("#low").val(),
				success:function(jobj, msg, xhr){
					alert(xhr.responseText);
					var list = jobj.list;
					var txt="";
					$.each(list, function(idx, obj){
						txt = txt+"sn : "+obj.bookSN+", 제목 : "+obj.name+", 저자 : "+obj.writer+", 출판사 : "+obj.publisher+
						"<a href='bookModify.do?sn="+obj.bookSN+"'>수정</a>&nbsp;&nbsp;"+
						"<a href='aaaaaaa'>삭제</a>"+"<br>";
					});
					$("#result_layer").html(txt);
				},
				error:function(xhr){
					alert("오류가 발생 :"+xhr.status);
				}
			});
		}else if($("#state").val()=="0"||$("#state").val()=="1"||$("#state").val()=="2"){
			$.ajax({
				url:"searchBookByState.do",
				type:"GET",
				dataType:"JSON",
				data:"state="+$("#state").val(),
				success:function(jobj, msg, xhr){
					var list = jobj.list;
					var txt="";
					$.each(list, function(idx, obj){
						txt = txt+"sn : "+obj.bookSN+", 제목 : "+obj.name+", 저자 : "+obj.writer+", 출판사 : "+obj.publisher+
						"<a href='res/admin/admin_book_manager_modify.jsp'>수정</a>&nbsp;&nbsp;"+
						"<a href='aaaaaaaaaaa'>삭제</a>"+"<br>";
					});
					$("#result_layer").html(txt);
				},
				error:function(xhr){
					alert("오류가 발생 :"+xhr.status);
				}
			});
		}
	});
});

</script>
</head>
<body>
		<select name="select" id="select">
			<option value="" >검색 기준 선택</option>
			<option value="all" >전체</option>
			<option value="sn" >S.N</option>
			<option value="name" >제목</option>
		 	<option value="writer" >저자</option>
		 	<option value="category" >카테고리</option>
		 	<option value="state" >상태</option>
		</select>
	<span id="first_layer"></span>
	<input type='button' value='조회' id='btn'/>
		<fieldset style="width:50%">
		<legend>조회결과</legend>
		<div id="result_layer"></div>
		</fieldset>
</body>
</html>